import { QFrame_App } from "../../ttapi_layer2/ttlayer2.js";
import { UserData, UserState } from "../game.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js"
import { tt } from "../../ttapi_interface/ttapi.js";
import { State_MainMenu } from "./state_mainmenu.js";
import { BattleItem, BattleMode } from "../model/battlelist.js";
import { State_Game } from "./state_game.js";

export class State_BattleChoose extends UserState {
    constructor(levelroot: BattleItem | null) {
        super()
        this._levelroot = levelroot;
    }
    private _levelroot: BattleItem;
    override OnInit(statemgr: QFrame_App<UserData>): void {
        super.OnInit(statemgr);
        this.InitAsync();
    }
    override  OnExit(): void {
        let _ui = this.userdata.scene.GetUI();
        _ui.removeChildAll();
    }
    private _inited: boolean = false;
    async InitAsync(): Promise<void> {
        let _ui = this.userdata.scene.GetUI();
        let _uiMidY = _ui.getWorldRect().Height / 2 - 20;

        {
            let scroll = new tt2.QUI_Panel_Scroll();
            let items = this.userdata.battlelist.GetList(this._levelroot);
            _ui.addChild(scroll);
            for (var i = 0; i < items.length; i++) {
                let it = items[i];
                let txt = it.name;
                if (it.mode == BattleMode.Battle) {
                    txt += "<战斗>";
                }
                if (it.mode == BattleMode.Level) {
                    txt += "<战役>";
                }
                let _btn1 = await this.CreateBtn(txt,1.5);
                _btn1.localRect.setVPosByTopBorder(40, i * 40);
                _btn1.localRect.setHPosFill();
                scroll.addChild(_btn1);

                if (it.mode == BattleMode.Battle) {
                    let _name = it.name;
                    let _config = it.config;
                    _btn1.OnClick = () => {
                        this.app.ChangeState(new State_Game(_config));
                    }
                }
                else if (it.mode == BattleMode.Level) {
                    let _name = it.name;
                    let _it = it;
                    _btn1.OnClick = () => {
                        this.app.ChangeState(new State_BattleChoose(_it));
                    }
                }

            }
            scroll.panelWidth = 200;
            scroll.panelHeight = 40 * scroll.getChildCount();
            scroll.localRect.setHPosByCenter(200);
            scroll.localRect.radioY1 = 0.5;
            scroll.localRect.radioY2 = 0.5;
            scroll.localRect.offsetY1 = -120;
            scroll.localRect.offsetY2 = 120;
        }

        {
            let l = new tt2.QUI_Label(this.app.font, "战役模式");
            l.localRect.setAsFill();
            l.halign = tt2.QUI_HAlign.Middle;
            l.fontScale.X = 1.5;
            l.fontScale.Y = 1.5;
            l.localRect.setVPosByTopBorder(20, 10);
            _ui.addChild(l);
        }
        {
            let l = new tt2.QUI_Label(this.app.font, "这个模式还没开发完");
            l.localRect.setVPosByTopBorder(20, _uiMidY - 100);
            l.color = new tt.Color(1, 0.8, 0, 1);
            _ui.addChild(l);
        }
        {
            let _foot = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("footer.png");
            let _imgfoot = new tt2.QUI_Image(_foot);
            _imgfoot.localRect.setHPosByCenter(240);
            _imgfoot.localRect.setVPosByBottomBorder(_foot.totalHeight, 0);
            _ui.addChild(_imgfoot);
        }

        {
            let _btn1 = await this.CreateBtn("退出", 1.0);
            _btn1.localRect.setHPosByLeftBorder(50, 10);
            _btn1.localRect.setVPosByTopBorder(20, 10);
            _ui.addChild(_btn1);
            _btn1.OnClick = () => {
                if (this._levelroot == null) {
                    this.app.ChangeState(new State_MainMenu());
                }
                else {
                    this.app.ChangeState(new State_BattleChoose(this._levelroot.parent));
                }
            }
        }


        this._inited = true;
    }
    async CreateBtn(text: string, fontscale: number = 2.0): Promise<tt2.QUI_Button> {
        let _btnborder = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("btnborder.png");
        let btn = new tt2.QUI_Button();
        btn.localRect.setHPosByCenter(160);
        btn.localRect.setVPosByBottomBorder(40, 30);
        let bimg: tt2.QUI_ImageScale9;
        {
            let s9 = new tt2.QUI_Scale9(_btnborder, new tt.Border(4, 4, 4, 4));
            let label = new tt2.QUI_Label(this.app.font, text);
            label.fontScale.X = fontscale;
            label.fontScale.Y = fontscale;
            label.color = new tt.Color(0.3, 0.3, 0.3, 1);
            bimg = new tt2.QUI_ImageScale9(s9);
            label.localRect.setAsFill();
            bimg.addChild(label);
            bimg.localRect.setAsFill();
        }
        let bimg2 = bimg.Clone() as tt2.QUI_ImageScale9;
        bimg2.color = new tt.Color(0.5, 0.5, 0.5, 1.0);

        //bimg2.localRect.setAsFill();
        btn.ElemNormal = bimg;
        btn.ElemPress = bimg2;
        return btn;
    }
}